import java.lang.constant.DynamicCallSiteDesc;
import java.util.Scanner;
import java.util.Random;
public class Main {
    /**
     判断年份是否为闰年
     */
    public static void leapYear() {
        Scanner scanner = new Scanner(System.in);
        System.out.print("输入年份：");
        int year = scanner.nextInt();
        // 此处完成代码
        if ((year%4 ==0 && year%100 !=0) || (year%400 ==0)) {
            System.out.println(year+"年是闰年");
        }else {
            System.out.println(year+"年不是闰年");
        }

        scanner.close();
    }

    /**
     判断成绩是`优秀`、`良好`、`中等`、`及格`、`不及格`
     */
    public static void grade() {
        Scanner scanner = new Scanner(System.in);
        System.out.print("输入分数：");
        int score = scanner.nextInt();
        // 此处完成代码
        if (score>=90){
            System.out.println(score+"是优秀");
        } else if (score>=80) {
            System.out.println(score+"是良好");
        } else if (score>=70) {
            System.out.println(score+"是中等");
        } else if (score>=60) {
            System.out.println(score+"是及格");
        } else {
            System.out.println(score+"是不及格");
        }

        scanner.close();
    }

    /**
     摄氏度、华氏度的转换
     */
    public static void convCF() {
        Scanner scanner = new Scanner(System.in);
        System.out.print("输入0是摄氏转华氏度，输入1是华氏转摄氏度：");
        int mode = scanner.nextInt();

        System.out.print("输入温度：");
        float o = scanner.nextFloat();
        // 此处完成代码
        if (mode==1){
            float c = (o - 32) * 5 / 9;
            System.out.print(o+"华氏度"+"="+c+"摄氏度");
        }
        if (mode==0){
            float f = o*9/5+32;
            System.out.print(o+"摄氏度"+"="+f+"华氏度");
        }

        scanner.close();
    }

    /**
     输出九九乘法表
     */
    public static void multiplicationTable() {
        // 此处完成代码
        for (int i = 1;i<10;i++){
            for (int j = 1;j<=i;j++){
                System.out.print(j+"*"+i+"="+i*j);
                System.out.print(" ");
            }System.out.println();
        }
    }

    /**
     求 1 ~ n 的和
     */
    public static void sum1ToN() {
        Scanner scanner = new Scanner(System.in);
        int sum = 0;
        System.out.print("输入n：");
        int n = scanner.nextInt();

        // 使用 for 循环方式，请注释掉 while 循环方式
        ;// 此处完成代码
        for (int i=1; i<=n; i++){
            sum +=i;
        }
        // 使用 while 循环方式，请注释掉 for 循环方式
        // 此处完成代码
        while (n>=0){
            sum +=n;
            n -=1;
        }

        System.out.printf("1 ~ %d之和：%d", n, sum);

        scanner.close();
    }

    /**
     输出 1 ~ n 所有能被 3 整除的数
     */
    public static void divisibleBy3() {
        Scanner scanner = new Scanner(System.in);
        System.out.print("输入n：");

        // 此处完成代码
        int n = scanner.nextInt();
        int j =0;
        for (int i=1; i<=n;i++){
            if (i % 3 == 0){
                System.out.print(i+"\t");
                j += 1;
            }
            if (j == 10){
                System.out.println();
                j = 0;
            }
        }

        scanner.close();
    }

    /**
     输出 100 ~ 999 所有水仙花数
     */
    public static void narcissisticNumber() {
        // 此处完成代码
        int sum = 0;
        int g = 0;
        int s = 0;
        int b = 0;

        for (char i=100; i<1000; i++){
            b = i/100;
            s = i%100/10;
            g = i%10;
            sum = (int) Math.pow(b,3)+(int) Math.pow(s,3)+(int) Math.pow(g,3);
            if (i==sum){
                System.out.println(sum);
            }
        }
    }

    /**
     输出 1 ~ n 所有质数
     */
    public static void primeNumber() {
        Scanner scanner = new Scanner(System.in);
        System.out.print("输入n：");
        int n = scanner.nextInt();

        // 此处完成代码
        int num = 0;
        for (int i=2 ;i<=n ;i++){
            boolean flag = true;
            for (int j=2; j<i ;j++){
                if (i % j == 0){
                    flag = false;
                    break;
                }
            }
            if (flag){
                System.out.print(i);
                System.out.print("\t");
                num +=1;
            }
            if (num == 10){
                System.out.println();
                num = 0;
            }
        }

        scanner.close();
    }
    /**
     猜数字游戏
     */
    public static void numberGuess() {
        Random random = new Random();
        int r = random.nextInt(100);
        System.out.println("数字已生成");

        Scanner scanner = new Scanner(System.in);

        // 此处完成代码
        int flag = 0;
        while (flag==0) {
            int num = scanner.nextInt();
            if (num == r) {
                System.out.println("猜中了");
                flag = 1;
            } else if (num<r) {
                System.out.println("小了");
            }else {
                System.out.println("大了");
            }

        scanner.close();
        }
    }
    /**
     输出斐波那契数列 - for 循环
     */
    public static void fibonacciFor() {
        Scanner scanner = new Scanner(System.in);
        System.out.print("输入n：");
        int n = scanner.nextInt();

        // 此处完成代码
        int[] num = new int[n+1];
        if (n !=0){
            num[0] = 1;
            num[1] = 1;
            for (int i=1; i<n; i++){
                num[i+1] =num[i]+num[i-1];
            }
            System.out.println(num[n-1]);
        }else {
            System.out.println(1);
        }

        scanner.close();
    }

    /**
     输出斐波那契数列 - 递归
     */
    public static int fibonacciRecursion(int n) {
        // 此处完成代码
        int[] num = new int[n+1];
        num[0] = 1;
        num[1] = 1;
        if (n<3){
            return 1;
        }
        return fibonacciRecursion(n-2) + fibonacciRecursion(n-1);

        //return 0; // 有必要的话这句也要改掉
    }

    /**
     输出斐波那契数列 - 调用递归
     */
    public static void fibonacci() {
        Scanner scanner = new Scanner(System.in);

        System.out.print("输入n：");

        int n = scanner.nextInt();

        int r = fibonacciRecursion(n);

        System.out.println(r);

        scanner.close();
    }

    /**
     计算阶乘 - for 循环
     */
    public static void factorialFor() {
        Scanner scanner = new Scanner(System.in);
        System.out.print("输入n：");
        int n = scanner.nextInt();

        int f = 1;
        // 此处完成代码

        for (int i=1 ; i<=n ; i++){
            f *=i;
        }

        System.out.println(f);
        scanner.close();
    }

    /**
     计算阶乘 - 递归
     */
    public static int factorialRecursion(int n) {
        if (n ==1){
            return 1;
        }
        return n*factorialRecursion(n-1);// 此处完成代码
        //return 0; // 有必要的话这句也要改掉
    }

    /**
     计算阶乘 - 调用递归
     */
    public static void factorial() {
        Scanner scanner = new Scanner(System.in);

        System.out.print("输入n：");

        int n = scanner.nextInt();

        int r = factorialRecursion(n);

        System.out.println(r);

        scanner.close();
    }

    /**
     统计字符串中英文字母、数字、空格的数量
     */
    public static void stringStatistic() {
        Scanner scanner = new Scanner(System.in);
        System.out.print("输入字符串，只包含英文字母、数字、空格：");
        String s = scanner.nextLine();

        int letters = 0;
        int digits = 0;
        int spaces = 0;

        // 此处完成代码
        for (int i=0; i<s.length(); i++){
            char S = s.charAt(i);
            if (Character.isLetter(S)){
                letters++;
            } else if (Character.isDigit(S)) {
                digits++;
            }else {
                spaces++;
            }
        }

        System.out.printf("字母有%d个\n", letters);
        System.out.printf("数字有%d个\n", digits);
        System.out.printf("空格有%d个", spaces);
        scanner.close();
    }

    /**
     检查一个字符串是否为回文
     */
    public static void palindrome() {
        Scanner scanner = new Scanner(System.in);
        System.out.print("输入字符串：");
        String s = scanner.nextLine();
        // 此处完成代码
        int left = 0;
        int right = s.length()-1;
        boolean flag = true;
        while (left < right){
            if (s.charAt(left) != s.charAt(right)){
                flag = false;
            }
            left++;
            right--;
        }
        System.out.println(flag?"字符串是回文":"字符串不是回文");

        scanner.close();
    }

    /**
     数组逆序输出
     */
    public static void reverseArray() {
        int[] numbers = randomInitIntArray(30, 0, 10);

        // 此处完成代码
        int left = 0;
        int right = numbers.length - 1;

        while (left < right) {
            int num = numbers[left];
            numbers[left] = numbers[right];
            numbers[right] = num;
            left++;
            right--;
        }
        printArray(numbers);
    }
    /**
     移动 0 至数组末尾
     */
    public static void move0ToEnd() {
        int[] numbers = randomInitIntArray(30, 0, 10);

        // 此处完成代码
        int n =0;
        for (int i=0; i<numbers.length; i++){
            if (numbers[i] == 0 && i != numbers.length-1){
                numbers[i] = numbers[i+1];
                n++;
            }
        }
        while (n==0){
            numbers[numbers.length-n] = 0;
            n--;
        }
        printArray(numbers);
    }

    /**
     数组的轮转
     */
    public static void rotateArray() {
        int[] numbers = randomInitIntArray(30, 0, 10);
        Scanner scanner = new Scanner(System.in);

        System.out.print("输入数组轮转的位置：");

        int m = scanner.nextInt();

        // 此处完成代码

        printArray(numbers);

        scanner.close();
    }

    /**
     最长公共前缀
     */
    public static void maxCommonPrefix() {
        Scanner scanner = new Scanner(System.in);

        System.out.print("输入第1个字符串：");
        String s1 = scanner.nextLine();
        System.out.print("输入第2个字符串：");
        String s2 = scanner.nextLine();

        // 此处完成代码
        boolean flag = false;
        StringBuilder S = new StringBuilder();
        int len = 0;

        if (s1.length()<s2.length()){
            len = s1.length();
        }else {
            len = s2.length();
        }
        for (int i=0; i<len; i++){
            if (s1.charAt(i) == s2.charAt(i)){
                S.append(s1.charAt(i));
                flag = true;
            }else {
                break;
            }
        }
        System.out.println(flag ? S : "没有公共前缀");
        scanner.close();
    }

    /**
     合并两个有序数组
     */
    public static void mergeArray() {
        int[] array1 = randomInitIntArray(10, 0, 10);
        int[] array2 = randomInitIntArray(20, 0, 10);

        bubbleSort(array1);
        bubbleSort(array2);

        System.out.print("排序后的array1：");
        printArray(array1);
        System.out.print("排序后的array2：");
        printArray(array2);

        int[] array = new int[array1.length + array2.length];

        // 此处完成代码
        for (int i=0; i<array2.length; i++){
            array[i] = array1[i];
            array[10+i] = array2[i];
        }
        int n = 20;
        while (n>0){

            n--;
        }
        printArray(array);
    }

    /**
     二维数组对角线元素求和
     */
    public static void sumDiagonal() {
        int[][] matrix = randomInitIntMatrix(10, 10, 0, 10);

        int sum = 0;

        // 此处完成代码

        System.out.println("矩阵对角线之和为：" + sum);
    }

    /**
     矩阵转置（二维数组行列互换）
     */
    public static void transposition() {
        int[][] matrix1 = randomInitIntMatrix(3, 4, 0, 10);
        int[][] matrix2 = randomInitIntMatrix(4, 3, 0, 1);

        // 此处完成代码

        System.out.println("转置后的矩阵：");
        printMatrix(matrix2);
    }

    public static void main(String[] args) {
    //leapYear();
    //    grade();
    //    convCF();
    //    multiplicationTable();
    //    sum1ToN();
    //    divisibleBy3();
    //    narcissisticNumber();
    //    primeNumber();
    //    numberGuess();
    //    fibonacciFor();
    //  fibonacci();
    //    factorialFor();
    //    factorial();
    //    stringStatistic();
    //    palindrome();
    //    reverseArray();
//        move0ToEnd();
//        rotateArray();
    //    maxCommonPrefix();
//        mergeArray();
        sumDiagonal();
//        transposition();
    }

    /**
     * 随机生成 int 数组
     * @param length: int (数组长度)
     * @param floor: int (数组中元素值的下限)
     * @param ceiling: int (数组中元素值的上限)
     * @return : int[] (初始化完成的数组)
     */
    public static int[] randomInitIntArray(int length, int floor, int ceiling) {
        Random random = new Random();
        int[] array = new int[length];

        for (int i = 0; i < length; i++) {
            array[i] = random.nextInt(ceiling - floor) + floor;
        }

        System.out.print("随机int数组：");
        printArray(array);

        return array;
    }

    /**
     * 随机生成2维 int 数组
     * @param m: int (2维数组的行数)
     * @param n: int (2维数组的列数)
     * @param floor: int (数组中元素值的下限)
     * @param ceiling: int (数组中元素值的上限)
     * @return : int[][] (初始化完成的2维数组)
     */
    public static int[][] randomInitIntMatrix(int m, int n, int floor, int ceiling) {
        Random random = new Random();
        int[][] matrix = new int[m][n];

        for (int i = 0; i < matrix.length; i++) {
            for (int j = 0; j < matrix[0].length; j++) {
                matrix[i][j] = random.nextInt(ceiling - floor) + floor;
            }
        }

        System.out.println("随机2维int数组：");
        printMatrix(matrix);

        return matrix;
    }

    /**
     * 打印 int 数组
     * @param array: int[] (要打印的int数组)
     */
    public static void printArray(int[] array) {
        for (int e : array) {
            System.out.print(e + " ");
        }
        System.out.println();
    }

    /**
     * 打印2维 int 数组
     * @param matrix: int[][] (要打印的2维int数组)
     */
    public static void printMatrix(int[][] matrix) {
        for (int[] array : matrix) {
            printArray(array);
        }
    }


    /**
     * 冒泡排序
     * @param array: int[] (要排序的int数组)
     */
    public static void bubbleSort(int[] array) {
        int n = array.length;
        boolean swapped;
        for (int i = 0; i < n - 1; i++) {
            swapped = false;
            for (int j = 0; j < n - i - 1; j++) {
                if (array[j] > array[j + 1]) {
                    // 交换 array[j+1] 和 array[j]
                    int temp = array[j];
                    array[j] = array[j + 1];
                    array[j + 1] = temp;
                    swapped = true;
                }
            }
            // 如果没有元素交换，说明数组已经排序好
            if (!swapped) break;
        }
    }
}